package com.gok.seasonmovie.controller;

import com.gok.seasonmovie.entity.Orders;
import com.gok.seasonmovie.entity.Seat;
import com.gok.seasonmovie.entity.SeatDTO;
import com.gok.seasonmovie.service.SeatService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/bookings")
public class SeatController {

    @Resource
    private SeatService seatService;
    @PostMapping("/getSeat")
    public Map<String, Object> getSeat(@RequestBody SeatDTO seatDTO) {
        try{
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("msg", "ok");
            List<Seat> seat = seatService.getSeat(seatDTO.getCid(),seatDTO.getMid(),seatDTO.getStarttime());
            response.put("data", seat);
            return response;
        }catch (RuntimeException e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 600);
            errorResponse.put("msg", "Database error: " + e.getMessage());
            return errorResponse;
        }
    }


    @PostMapping("/insertSeats")
    public Map<String, Object> insertSeats(@RequestBody Map<String, Object> requestData) {

        try {
            String cid = requestData.get("cid").toString();
            String mid = requestData.get("mid").toString();
            String starttime = (String) requestData.get("starttime");
            List<Integer> seats = (List<Integer>) requestData.get("seats");
            seatService.insertSeats(cid, mid, starttime, seats);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("msg", "ok");
            return response;
        } catch (RuntimeException e) {
            // 这里捕获了MyBatis可能抛出的运行时异常
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 600);
            errorResponse.put("msg", "Database error: " + e.getMessage());
            return errorResponse;
        }
    }
}
